/*
 * eeprom.c
 *
 *  Created on: 27-06-2012
 *      Author: Mati
 */
#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include "eeprom.h"
#include "../I2C/i2c.h"


int SaveBytesInEEPROM(uint8_t *bytes, uint8_t start_addr, int num)
{
  uint32_t i;

  if ( I2CInit( (uint32_t)I2CMASTER ) == 0 )	/* initialize I2c */
  {
	return 1;				/* Fatal error */
  }
  //printf("Do funkcji przekazano tablice: ");
  for ( i = 0; i < num; i++ )
  {
	  I2CMasterBuffer[i+2] = bytes[i];
	  //printf("%X ",bytes[i]);
  }
  //printf("\n\r");
  I2CWriteLength = 2+num;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = MC24LC64_ADDR;
  I2CMasterBuffer[1] = start_addr;		/* address LSB */

  uint32_t fin_state = I2CEngine();


  for ( i = 0; i < num; i++ )
  {
	  I2CSlaveBuffer[i] = 0x00;
  }

  i=0;
  while (I2CEngine() == I2CSTATE_SLA_NACK)
  {
	  i++;
  }

  return 0;
}

int SaveByteInEEPROM(uint8_t byte2write, uint8_t addr)
{
  uint32_t i;

  if ( I2CInit( (uint32_t)I2CMASTER ) == 0 )	/* initialize I2c */
  {
	return 1;				/* Fatal error */
  }

  I2CWriteLength = 3;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = MC24LC64_ADDR;
  //I2CMasterBuffer[1] = (addr&0xF0);		/* address MSB */
  I2CMasterBuffer[1] = addr;		/* address LSB */
  I2CMasterBuffer[2] = byte2write;		/* Data0 */
  uint32_t fin_state = I2CEngine();

  for ( i = 0; i < 1; i++ )
  {
	I2CSlaveBuffer[i] = 0x00;
  }

  i=0;
  while (I2CEngine() == I2CSTATE_SLA_NACK)
  {
	  i++;
  }

  return 0;
}

uint8_t ReadByteFromEEPROM(uint8_t addr)
{
	  uint32_t i;

	  if ( I2CInit( (uint32_t)I2CMASTER ) == 0 )	/* initialize I2c */
	  {
		return 1;				/* Fatal error */
	  }
//	  for ( i = 0; i < BUFSIZE; i++ )
//	  {
//		I2CSlaveBuffer[i] = 0x00;
//	  }
	  /* Write SLA(W), address, SLA(R), and read 9 bytes back. */

	  I2CWriteLength = 0;
	  I2CReadLength = 1;
	  I2CMasterBuffer[0] = MC24LC64_ADDR;
	  I2CMasterBuffer[1] = addr;		/* address MSB */
	  //I2CMasterBuffer[2] = 0x00;		/* address LSB */
	  I2CMasterBuffer[2] = MC24LC64_ADDR | RD_BIT;

	  i=0;
	  while (I2CEngine() == I2CSTATE_SLA_NACK)
		  i++;

	  return I2CSlaveBuffer[0];
}

void ReadBytesFromEEPROM(uint8_t addr, uint8_t num, uint8_t *tab)
{
	  uint32_t i;

	  if ( I2CInit( (uint32_t)I2CMASTER ) == 0 )	/* initialize I2c */
	  {
		return 1;				/* Fatal error */
	  }
//	  for ( i = 0; i < BUFSIZE; i++ )
//	  {
//		I2CSlaveBuffer[i] = 0x00;
//	  }
	  /* Write SLA(W), address, SLA(R), and read 9 bytes back. */

	  I2CWriteLength = 3;
	  I2CReadLength = num;
	  I2CMasterBuffer[0] = MC24LC64_ADDR;
	  I2CMasterBuffer[1] = addr;		/* address MSB */
	  I2CMasterBuffer[2] = 0x00;		/* address LSB */
	  I2CMasterBuffer[3] = MC24LC64_ADDR | RD_BIT;

	  i=0;
	  while (I2CEngine() == I2CSTATE_SLA_NACK)
		  i++;

	  for(i=0;i<num;i++)
	  {
		  *(tab++) = I2CSlaveBuffer[i];
	  }

}

int i2ctest_wr (void)
{
	  /* Basic chip initialization is taken care of in SystemInit() called
	   * from the startup code. SystemInit() and chip settings are defined
	   * in the CMSIS system_<part family>.c file.
	   */

  uint32_t i;

  if ( I2CInit( (uint32_t)I2CMASTER ) == 0 )	/* initialize I2c */
  {
	return 1;				/* Fatal error */
  }
  /* In order to start the I2CEngine, the all the parameters
  must be set in advance, including I2CWriteLength, I2CReadLength,
  I2CCmd, and the I2cMasterBuffer which contains the stream
  command/data to the I2c slave device.
  (1) If it's a I2C write only, the number of bytes to be written is
  I2CWriteLength, I2CReadLength is zero, the content will be filled
  in the I2CMasterBuffer.
  (2) If it's a I2C read only, the number of bytes to be read is
  I2CReadLength, I2CWriteLength is 0, the read value will be filled
  in the I2CMasterBuffer.
  (3) If it's a I2C Write/Read with repeated start, specify the
  I2CWriteLength, fill the content of bytes to be written in
  I2CMasterBuffer, specify the I2CReadLength, after the repeated
  start and the device address with RD bit set, the content of the
  reading will be filled in I2CMasterBuffer index at
  I2CMasterBuffer[I2CWriteLength+2].

  e.g. Start, DevAddr(W), WRByte1...WRByteN, Repeated-Start, DevAddr(R),
  RDByte1...RDByteN Stop. The content of the reading will be filled
  after (I2CWriteLength + two devaddr) bytes. */

  /* Write SLA(W), address and one data byte */
//#if 1
  I2CWriteLength = 253;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = MC24LC64_ADDR;
  I2CMasterBuffer[1] = 0x00;		/* address MSB */
  I2CMasterBuffer[2] = 0x00;		/* address LSB */

  for(i=0;i<250;i++)
  {
	  I2CMasterBuffer[i+3] = 256-i;
  }
//  I2CMasterBuffer[3] = 0x55;		/* Data0 */
//  I2CMasterBuffer[4] = 0xAA;		/* Data1 */
//  I2CMasterBuffer[5] = 0x12;		/* Data2 */
//  I2CMasterBuffer[6] = 0x34;		/* Data3 */
//  I2CMasterBuffer[7] = 0xFF;		/* Data4 */
//  I2CMasterBuffer[8] = 0xBB;		/* Data5 */
//  I2CMasterBuffer[9] = 0xCC;		/* Data6 */
  //uint32_t fin_state = I2CEngine();
//#endif
//  for ( i = 0; i < BUFSIZE; i++ )
//  {
//	I2CSlaveBuffer[i] = 0x00;
//  }
  /* Write SLA(W), address, SLA(R), and read 4 bytes back. */

//  I2CWriteLength = 3;
//  I2CReadLength = 4;
//  I2CMasterBuffer[0] = MC24LC64_ADDR;
//  I2CMasterBuffer[1] = 0x00;		/* address MSB */
//  I2CMasterBuffer[2] = 0x00;		/* address LSB */
//  I2CMasterBuffer[3] = MC24LC64_ADDR | RD_BIT;

  i=0;
  while (I2CEngine() == I2CSTATE_SLA_NACK)
  {
	  i++;
  }

  return 0;
}

int i2ctest_rd (void)
{
	  uint32_t i;

	  if ( I2CInit( (uint32_t)I2CMASTER ) == 0 )	/* initialize I2c */
	  {
		return 1;				/* Fatal error */
	  }
//	  for ( i = 0; i < BUFSIZE; i++ )
//	  {
//		I2CSlaveBuffer[i] = 0x00;
//	  }
	  /* Write SLA(W), address, SLA(R), and read 9 bytes back. */

	  I2CWriteLength = 3;
	  I2CReadLength = 256;
	  I2CMasterBuffer[0] = MC24LC64_ADDR;
	  I2CMasterBuffer[1] = 0x00;		/* address MSB */
	  I2CMasterBuffer[2] = 0x00;		/* address MSB */
	  I2CMasterBuffer[3] = MC24LC64_ADDR | RD_BIT;

	  i=0;
	  while (I2CEngine() == I2CSTATE_SLA_NACK)
		  i++;

	  return 0;
}
/******************************************************************************
**                            End Of File
******************************************************************************/
